local skel = fk.CreateSkill {
  name = "rmt__linli",
}

Fk:loadTranslationTable{
  ["rmt__linli"] = "遴丽",
  [":rmt__linli"] = "至少两张锦囊牌进入弃牌堆时，你可展示手牌，若点数均小于之，你可将之收为手牌。连续发动时，弃置一张上次以此法获得的牌。",

  ["#rmt__linli-invoke"] = "遴丽：你可以展示手牌，获得进入弃牌堆的锦囊牌",
  ["#rmt__linli-none"] = "遴丽：你可以展示手牌",
  ["@@rmt__linli-inhand"] = "遴丽",
  ["#rmt__linli-discard"] = "遴丽：请弃置一张因此获得的牌。",
}

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    local ids = {}
    local min = 999
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).type == Card.TypeTrick then
            min = math.min(Fk:getCardById(info.cardId).number, min)
            table.insert(ids, info.cardId)
          end
        end
      end
    end
    if #ids >= 2 then
      event:setCostData(self, { cards = ids, min = min })
      return true
    end
  end,
  on_cost = function (self, event, target, player, data)
    local min = event:getCostData(self).min
    local prompt = "#rmt__linli-none"
    if table.every(player:getCardIds("h"), function (id)
      return Fk:getCardById(id).number <= min
    end) then
      prompt = "#rmt__linli-invoke"
    end
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = prompt })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local min, discards = event:getCostData(self).min, event:getCostData(self).cards
    -- 获取你上一次发动的技能
    local throw = false
    local currentEvent = player.room.logic:getCurrentEvent()
    local lastEvent = player.room.logic:getEventsByRule(GameEvent.SkillEffect, 1, function (e)
      local skill = e.data.skill
      return e.id < currentEvent.id and e.data.who == player and skill and skill:isPlayerSkill(player)
      and not skill.is_delay_effect
    end, 1)[1]
    if lastEvent and lastEvent.data.skill.name == skel.name then
      throw = true
    end
    local get = true
    if not player:isKongcheng() then
      local hand = player:getCardIds("h")
      player:showCards(hand)
      local max = 0
      for _, id in ipairs (hand) do
        max = math.max(max, Fk:getCardById(id).number)
      end
      if max >= min then
        get = false
      end
    end
    if throw then
      local cards = table.filter(player:getCardIds("h"), function (id)
        return Fk:getCardById(id):getMark("@@rmt__linli-inhand") ~= 0 and not player:prohibitDiscard(id)
      end)
      if #cards > 0 then
        cards = room:askToDiscard(player, {
          min_num = 1, max_num = 1, skill_name = skel.name, cancelable = false, pattern = tostring(Exppattern{ id = cards }),
          prompt = "#rmt__linli-discard", skip = false,
        })
      end
    end
    local ids = table.filter(discards, function (id)
      return room:getCardArea(id) == Card.DiscardPile
    end)
    if get and not player.dead and #ids > 0 then
      local choices = {"rmt__linli-takeall","Cancel"}
      local choice = room:askToChoice(player, { choices = choices, skill_name = skel.name, prompt = "#rmt__linli-choice" })
      if choice == "rmt__linli-takeall" then
        for _, id in ipairs (player:getCardIds("h")) do
          if Fk:getCardById(id):getMark("@@rmt__linli-inhand") ~= 0 then
            room:setCardMark(Fk:getCardById(id), "@@rmt__linli-inhand", 0)
          end
        end
        room:obtainCard(player, ids, true, fk.ReasonPrey, player, skel.name, "@@rmt__linli-inhand")
      end
    end
  end,
})



return skel
